package com.big_data_studio.controller;


import com.big_data_studio.dto.RegisterParm;
import com.big_data_studio.dto.UserParm;
import com.big_data_studio.entity.user.User;
import com.big_data_studio.service.IUserService;
import com.big_data_studio.util.general.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiao_jie
 * @since 2022-09-12
 */
@RestController
@RequestMapping("/user")
public class UserController {

        @Autowired
        private IUserService service;

        // 登录
        @CrossOrigin("true")
        @PostMapping("/login")
        public R userLogin(@RequestBody User user, HttpServletRequest request) {
            return service.userLogin(user,request);
        }
        // 获取邮箱验证码
        @GetMapping("/getCode")
        public R sendQQMailCode(String mail){
            return service.sendQQMailCode(mail);
        }

        // 注册
        @PostMapping("/register")
        public R sendQQMail(@RequestBody RegisterParm registerParm){
            System.out.println(registerParm);
            return service.register(registerParm);
        }

        // 退出
        @GetMapping("/logout")
        public R userLogout(HttpServletRequest request){
            String token = request.getHeader("token");
            return service.userLogOut(token);
        }

        // 获取某个用户详细信息
//        @PreAuthorize("hasAuthority('test01')")
        @GetMapping("/getOne")
        public R getUser(Integer id) {
            return service.getOne(id);
        }

        // 分页查询
        @PostMapping("list")
//        @PreAuthorize("hasAuthority('test')")
        public R list(@RequestBody UserParm userParm){
            return service.list(userParm);
        }

        // 添加
        @PostMapping("add")
        public R add(@RequestBody User user){
            return service.add(user);
        }

        // 更新
        @PostMapping("update")
        public R update(@RequestBody User user){
            return service.update(user);
        }

        // 删除
        @GetMapping("del")
        public R del(Integer id){
            return service.del(id);
        }
}
